package kr.hkit.powerjava.chapter15.menu;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.SwingConstants;

import java.awt.BorderLayout;

import javax.swing.JTextField;

import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class InputEmp extends JPanel implements ActionListener {
	private JLabel lblTitle;
	private JPanel panel;
	private JLabel lblNum;
	private JTextField txtNum;
	private JLabel lblName;
	private JTextField txtName;
	private JTextField txtTitle;
	private JLabel lblMgn;
	private JTextField txtMgn;
	private JLabel lblSalary;
	private JTextField txtSalary;
	private JLabel lblDno;
	private JTextField txtDno;
	private JButton btnNewButton;

	/**
	 * Create the panel.
	 */
	public InputEmp() {

		initialize();
	}
	private void initialize() {
		setLayout(new BorderLayout(0, 0));
		
		btnNewButton = new JButton("추가");
		add(btnNewButton, BorderLayout.SOUTH);
		btnNewButton.addActionListener(this);
		
		panel = new JPanel();
		panel.setLayout(new GridLayout(6, 2, 0, 2));
		
		lblNum = new JLabel("사원번호");
		lblNum.setHorizontalAlignment(SwingConstants.CENTER);
		panel.add(lblNum);
		
		txtNum = new JTextField();
		panel.add(txtNum);
		txtNum.setColumns(10);
		
		lblName = new JLabel("사원명");
		lblName.setHorizontalAlignment(SwingConstants.CENTER);
		panel.add(lblName);
		
		txtName = new JTextField();
		panel.add(txtName);
		txtName.setColumns(10);
		
		lblTitle = new JLabel("직책");
		lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
		panel.add(lblTitle);
		
		txtTitle = new JTextField();
		panel.add(txtTitle);
		txtTitle.setColumns(10);
		
		lblMgn = new JLabel("상급자");
		lblMgn.setHorizontalAlignment(SwingConstants.CENTER);
		panel.add(lblMgn);
		
		txtMgn = new JTextField();
		panel.add(txtMgn);
		txtMgn.setColumns(10);
		
		lblSalary = new JLabel("급여");
		lblSalary.setHorizontalAlignment(SwingConstants.CENTER);
		panel.add(lblSalary);
		
		txtSalary = new JTextField();
		panel.add(txtSalary);
		txtSalary.setColumns(10);
		
		lblDno = new JLabel("부서번호");
		lblDno.setHorizontalAlignment(SwingConstants.CENTER);
		panel.add(lblDno);
		
		txtDno = new JTextField();
		panel.add(txtDno);
		txtDno.setColumns(10);
	}
	
	public String getLblNewLabel() {
		return lblTitle.getText();
	}
	
	public void setLblNewLabel(String title) {
		this.lblTitle.setText(title);;
	}
	
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnNewButton) {
			actionPerformedBtnNewButton(e);
		}
	}
	
	private boolean inValidCheck(){
		//공백반 체크
		if(txtNum.getText().equals("")){
			txtNum.requestFocus();
			return false;
		}
		if(txtName.getText().equals("")){
			txtName.requestFocus();
			return false;
		}
		return true;
		
	}
	
	protected void actionPerformedBtnNewButton(ActionEvent e) {
		try{
			JOptionPane.showMessageDialog(null, getInfo().toString());
		}catch(NullPointerException e1){
			JOptionPane.showMessageDialog(null, "잘못된 정보");
		}
		
	}
	private Object getInfo() {
		if (inValidCheck()){ 
			int empNo = Integer.parseInt(txtNum.getText().trim());
			String empName = txtName.getText().trim();
			String title = txtTitle.getText().trim();
			int manager = Integer.parseInt(txtMgn.getText().trim());
			int salary = Integer.parseInt(txtSalary.getText().trim());
			int dno = Integer.parseInt(txtDno.getText().trim());
			return new Employee(empNo, empName, title, manager, salary, dno);
		}else{
			return null;
		}
	}
	
	
}
